/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.business.security.boundary;

import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.Invite;
import it.polimi.registration.business.security.entity.InvitePK;
import it.polimi.registration.business.security.entity.User;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Stefano
 */
@Stateless
public class InviteManager {

    @PersistenceContext
    EntityManager em;
    
    @Inject 
    private UserManager um;

    public void save(Invite invite) {
        em.persist(invite);
    }
    
    public void delete(Invite invite){
        Invite i = em.find(Invite.class, invite.getInvitePK());
        em.remove(i);
        em.flush();
    }
    
    public List<Invite> findInvites(User user){
        TypedQuery<Invite> query = em.createQuery("SELECT i FROM Invite i Where i.user = :mail AND i.event.endTime > :today", Invite.class);
            query.setParameter("mail", em.find(User.class, user.getEmail()));
            query.setParameter("today", Calendar.getInstance());
        return query.getResultList();
    }
    
    public List<Invite> findInvites(User user,Event event){
        TypedQuery<Invite> query = em.createQuery("SELECT i FROM Invite i Where i.user = :mail AND i.event = :event", Invite.class);
                query.setParameter("mail", em.find(User.class, user.getEmail()));
                query.setParameter("event", em.find(Event.class, event.getId()));
        return query.getResultList();
    }
    
    public Invite getInvite(InvitePK invitePK){
        return em.find(Invite.class, invitePK);        
    }
    
    public void update(Invite invite){
        Invite i = em.find(Invite.class, invite.getInvitePK());
        i.setStatus(invite.getStatus());
        em.merge(i);
    }
    
    
    public List<Event> findAcceptedEvents(User user){
        TypedQuery<Invite> query = em.createQuery("SELECT i FROM Invite i WHERE i.user = :mail AND i.status = 1", Invite.class);
        query.setParameter("mail", em.find(User.class, user.getEmail()));
        List<Event> events = new ArrayList<>();
        for(Invite i : query.getResultList()){
            events.add(i.getEvent());
        }
        return events;
    }
    
    public List<User> findParticipants(Event event){
        TypedQuery<Invite> query = em.createQuery("SELECT i FROM Invite i WHERE i.event = :e AND i.status = 1", Invite.class);
        query.setParameter("e", em.find(Event.class, event.getId()));
        List<User> users = new ArrayList<>();
        for(Invite i : query.getResultList()){
            users.add(i.getUser());
        }
        return users;
    }
        
    public List<User> findInvited(Event event){
        TypedQuery<Invite> query = em.createQuery("SELECT i FROM Invite i WHERE i.event = :e", Invite.class);
        query.setParameter("e", em.find(Event.class, event.getId()));
        List<User> users = new ArrayList<>();
        for(Invite i : query.getResultList()){
            users.add(i.getUser());
        }
        return users;
    }
    
    

}
